package com.ruoyi.neo.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.neo.repository.PartCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.neo.mapper.PartCategoryMapper;
import com.ruoyi.neo.domain.PartCategory;
import com.ruoyi.neo.service.IPartCategoryService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 零件分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-09
 */
@Service
public class PartCategoryServiceImpl implements IPartCategoryService 
{
    @Resource
    private PartCategoryMapper partCategoryMapper;
    @Resource
    private PartCategoryRepository partCategoryRepository;

    /**
     * 查询零件分类
     * 
     * @param categoryId 零件分类主键
     * @return 零件分类
     */
    @Override
    public PartCategory selectPartCategoryByCategoryId(Long categoryId)
    {
        return partCategoryRepository.findCategoryByIdInNeo4j(categoryId);
    }

    /**
     * 查询零件分类列表
     * 
     * @param partCategory 零件分类
     * @return 零件分类
     */
    @Override
    public List<PartCategory> selectPartCategoryList(PartCategory partCategory)
    {
        return partCategoryRepository.findAllCategoriesInNeo4j(partCategory.getCategoryName(), partCategory.getParentCategoryId(),partCategory.getDescription());
    }

    /**
     * 新增零件分类
     * 
     * @param partCategory 零件分类
     * @return 结果
     */
    @Override
    public int insertPartCategory(PartCategory partCategory)
    {
//        String categoryName = partCategory.getCategoryName();
//        partCategory.setCreateTime(DateUtils.getNowDate());
        Long nodeId = partCategoryRepository.addCategoryToNeo4j(partCategory.getCategoryId(),partCategory.getCategoryName(),partCategory.getParentCategoryId(),partCategory.getDescription());
        return nodeId != null ? 1 : 0;
    }

    /**
     * 修改零件分类
     * 
     * @param partCategory 零件分类
     * @return 结果
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePartCategoryInMysql(PartCategory partCategory) {
        partCategory.setUpdateTime(DateUtils.getNowDate());
        return partCategoryMapper.updatePartCategory(partCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePartCategoryInNeo4j(PartCategory partCategory) {
        // 查找现有分类
        PartCategory existingCategory = partCategoryRepository.findCategoryByIdInNeo4j(partCategory.getCategoryId());
        if (existingCategory == null) {
            throw new RuntimeException("Category not found for id: " + partCategory.getCategoryId());
        }

        // 获取旧分类名称
        String oldName = existingCategory.getCategoryName();
        System.out.println(oldName);

        // 更新分类
        int count = partCategoryRepository.updateCategoryToNeo4j(
                oldName,
                partCategory.getCategoryName(),
                partCategory.getParentCategoryId(),
                partCategory.getDescription()
        );
        return count;
    }

    /**
     * 批量删除零件分类
     */
    @Override
    public int deletePartCategoryByCategoryNames(String[] categoryNames)
    {
        try {
            int mysqlResult = deletePartCategoryByCategoryNamesInMysql(categoryNames);
            if(mysqlResult<=0){
                throw new RuntimeException("MySQL 删除失败，没有记录被删除");
            }
            int neo4jResult = deletPartCategoryByCategoryNamesInNeo4j(categoryNames);
            if(neo4jResult<=0){
                throw new RuntimeException("neo4j 删除失败，没有记录被删除");
            }
            return mysqlResult;
        }catch (Exception e){
                throw new RuntimeException("删除操作已经失败，已经回滚!");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePartCategoryByCategoryNamesInMysql(String[] categoryNames) {
        return partCategoryMapper.deletePartCategoryByCategoryNames(categoryNames);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletPartCategoryByCategoryNamesInNeo4j(String[] categoryNames) {
        try{
            int count  = partCategoryRepository.deleteCategoryInNeo4j(categoryNames);
            return count;
        }catch (Exception e){
            return 0;
        }
    }

    /**
     * 删除零件分类信息
     * 
     * @param categoryId 零件分类主键
     * @return 结果
     */
    @Override
    public int deletePartCategoryByCategoryId(Long categoryId)
    {
        return partCategoryMapper.deletePartCategoryByCategoryId(categoryId);
    }

    @Override
    public List<PartCategory> selectPartCategoryInNeo4j(PartCategory partCategory) {
        return partCategoryRepository.findAllCategoriesInNeo4j(partCategory.getCategoryName(),partCategory.getParentCategoryId(),partCategory.getDescription());
    }

//    @Override
//    public List<PartCategory> selectPartCategoryInNeo4j() {
//        return partCategoryRepository.selectAllPartCartegroy();
//    }
}
